import {
  request,
  BASEURL
} from '../../utils/request';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    codeIMG: "../../image/tb148.png",
    goodsMnie: {},
    money: '',
    chooseImg:''
  },


  onLoad: function (options) {

  },
  //form表单
  async submitForm(e) {
    var that = this
    let {
      openid
    } = wx.getStorageSync('uid')
    let {
      jine
    } = e.detail.value
    var chooseImg=that.data.chooseImg;
    //验证信息
    if (jine == '') {
      wx.showToast({
        title: '请输入提现金额',
        icon: 'none',
        duration:1500
      })
    } else if (jine < 1) {
      wx.showToast({
        title: '金额不能小于1',
        icon:'none',
        duration:1500
      })
    }else if(!chooseImg || chooseImg == ""){
      wx.showToast({
        title: '请上传收款码',
        icon:"none",
        duration:1500
      })
      return false;
    }else {
      //发请求
      let res = await request({
        url: '/api/wxapp/aliwithdraw',
        method: 'POST',
        data: {
          openid,
          skcode:this.data.chooseImg,
          amount: jine
        },
      })
      if (res.data.code == 1) {
        this.userInfo()
        this.setData({
          money: '',
          codeIMG:'../../image/tb148.png'
        })
        wx.showToast({
          title: '提现成功'
        })
        setTimeout(()=>{
          wx.switchTab({
            url: '/pages/index/index',
          })
        },2000)
      }else if(res.data.code==0){
        wx.showToast({
          title: res.data.msg,
          icon:"none"
        })
      }
    }
    
  },
  // 图片上传
  chooseImg: function () {
    var that=this;
    wx.chooseImage({
      count: 1, 
      sizeType: ['original', 'compressed'], 
      sourceType: ['album', 'camera'], 
      success: function (res) {
        // console.log(res.tempFilePaths[0],"上传图片路径")
        that.setData({
          codeIMG: res.tempFilePaths[0]
        })
        wx.uploadFile({
          filePath: res.tempFilePaths[0],
          name: 'file',
          //1. 图片接口
          url: BASEURL + '/api/wxapp/upfile',
          success:function(res){
            // console.log(res,"图片上传")
            //2.
            let data = JSON.parse(res.data)
            // console.log(data)
            that.setData({
              chooseImg:data.data.file
            })
          }
        })
      }
    })
  },
  //获取余额
  async userInfo() {
    let {
      openid
    } = wx.getStorageSync('uid')
    let res = await request({
      url: '/api/Wxapp/userInfo',
      method: "POST",
      data: {
        openid
      }
    })
    // console.log(res)
    let goodsMnie = res.data.data
    this.setData({
      goodsMnie,
      BASEURL
    })
  },
  // 全部提现
  change() {
    let money = this.data.goodsMnie.profit
    this.setData({
      money
    })
  },
  onShow: function () {
    this.userInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let {openid} = wx.getStorageSync('uid')
    return {
      title: "邀请好友",
      path:`/pages/index/index?pid=${openid}` 
    }
  }
})